﻿/// <reference path="class.js" />
/// <reference path="cryptojs-sha1.js" />
/// <reference path="jquery-2.0.2.js" />
/// <reference path="http-requester.js" />

var dataPersister = (function () {
    var nickname = localStorage.getItem("Username");
    var sessionKey = localStorage.getItem("SessionKey");

    var saveToLocalStorage = function (nick, session) {
        localStorage.setItem("Username", nick);
        localStorage.setItem("SessionKey", session);
        nickname = localStorage.getItem("Username");
        sessionKey = localStorage.getItem("SessionKey");
    }

    var clearLocalStorage = function () {
        localStorage.removeItem("Username");
        localStorage.removeItem("SessionKey");
        nickname = null;
        sessionKey = null;
    }

    var MainPersister = Class.create({
        init: function (rootUrl) {
            this.rootUrl = rootUrl;
            this.user = new UserPersister(this.rootUrl);
            this.message = new MessagePersister(this.rootUrl);
        },

        isLoggedIn: function () {
            return nickname != null && sessionKey != null;
        },

        getNickname: function () {
            return nickname;
        }
    });

    var UserPersister = Class.create({
        init: function (url) {
            this.rootUrl = url + "user";
        },

        login: function (user, success, error) {
            var url = this.rootUrl + "/login";
            var userData = {
                username: user.username,
                password: CryptoJS.SHA1(user.username + user.password).toString()
            }

            httpRequester.makePostRequest(url, userData, function (result) {
                saveToLocalStorage(result.Username, result.SessionKey);
                success(result);
            }, error);
        },

        register: function (user, success, error) {
            var url = this.rootUrl + "/register";
            var userData = {
                username: user.username,
                password: CryptoJS.SHA1(user.username + user.password).toString()
            }

            httpRequester.makePostRequest(url, userData, function (result) {
                saveToLocalStorage(result.Username, result.SessionKey);
                success(result);
            }, error);
        },

        logout: function (success, error) {
            var url = this.rootUrl + "/logout/" + sessionKey;
            httpRequester.makeGetRequest(url, function (result) {
                clearLocalStorage();
                success(result);
            }, error);
        },

        users: function (success, error) {
            var url = this.rootUrl;
            httpRequester.makeGetRequest(url, success, error);
        },
    });

    var MessagePersister = Class.create({
        init: function (url) {
            this.rootUrl = url + 'message/';
        },
        send: function (data, success, error) {
            var url = this.rootUrl + 'post';
            httpRequester.makePostRequest(url, data, success, error);
        }
    });

    return {
        getPersister: function (url) {
            return new MainPersister(url);
        }
    }
}());